var messageSupportObj = {
	markMessageRead : function(messageID) {
		var params = 'handler=markMessageRead&messageID='+messageID;
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,
			onSuccess: function(transport) {
				$('messageContainerDiv'+messageID).className = 'grayText';	
				/*
				 * Reset message panel title
				 */
				Ext.getCmp('panelMessages').setTitle('Messages ('+transport.responseText+' unread)');
			}
		});
	},
	openNewMessageForm : function() {
		// Display add form
		new Effect.SlideDown('newMessageForm', {direction: 'top', duration: .3});
	},
	cancelNewMessageForm : function() {
		this.clearNewMessageForm();
		new Effect.SlideUp('newMessageForm', {direction: 'top', duration: .3});
	},
	clearNewMessageForm : function() {
		$('messageText').value = '';
	},
	getStaff : function() {
		var params = 'handler=getStaff&renderType=json';
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,
			onSuccess: function(transport) {
				messageSupportObj.renderStaffToSelect(transport.responseText);				
			}
		});
	},
	renderStaffToSelect : function(stringJSON) {
		var jsonObject = eval('('+stringJSON+')');
		var totalStaff  = (jsonObject.Staff.length)-1;
		for(a=0;a<=totalStaff;a++) {
			var staffID = jsonObject.Staff[a].StaffID;
			var staffName = jsonObject.Staff[a].NameFirst+' '+jsonObject.Staff[a].NameLast;
			$('messageTo').options[a+1] = new Option(staffName,staffID);
		}
	}
}

var saveMessageObj = {
	saveMessage : function() {
		var dataString = {
			staffID:    $F('messageTo'),
			message:    $F('messageText')
		};	
		
		jsonString = Object.toJSON(dataString);
		
		var params = 'handler=saveMessage2&jsonString='+jsonString;
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,
			onSuccess: function(transport) {
				updateStatus('Message sent','success','messageMessage');
			}
		});	
	}
}

var getMessageObj = {
	getMessages : function(itemID,itemType) {
		updateStatus('Loading messages...','wait','messageWindowStatus');
		var params = 'handler=getMessages&itemID='+itemID+'&itemType='+itemType;
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,
			onSuccess: function(transport) {
				renderMessageObj.renderMessagesWindow(transport.responseText);
				updateStatus('','clearStatus','messageWindowStatus');
			},
			onFailure: function() {
				alert('Oops!');
			}	
		});
	}
}	

var renderMessageObj = {
	renderMessagesWindow : function(stringJSON) {
		var jsonObject = eval('('+stringJSON+')');
		var rowClass = '';
		var stringOut = '';
		var qTipText;
		var totalNotes  = (jsonObject.Notes.length)-1;
		var unreadNotes = jsonObject.Unread;
		Ext.getCmp('panelMessages').setTitle('Messages ('+unreadNotes+' unread)');
		
		if (totalNotes>=0) {			
			for(a=0;a<=totalNotes;a++) {
				var noteID        = jsonObject.Notes[a].MessageID;
				var noteDateEntry = jsonObject.Notes[a].DateEntry;
				var noteText      = jsonObject.Notes[a].Message;
				var contactID     = jsonObject.Notes[a].ContactID;
				var followUpID    = jsonObject.Notes[a].FollowUpID;
				var staffFName    = jsonObject.Notes[a].StaffFName;
				var staffLName    = jsonObject.Notes[a].StaffLName;
				var staffEmail    = jsonObject.Notes[a].StaffEmail;
				var staffPhone    = jsonObject.Notes[a].StaffPhone;
				var staffUserid   = jsonObject.Notes[a].Userid;
				var staffRole     = jsonObject.Notes[a].StaffRole;
				var messageRead   = jsonObject.Notes[a].MessageRead;
				var readDateTime  = jsonObject.Notes[a].ReadDateTime;				
								
				if (messageRead == 1) {
					var messageContainerClass = 'grayText';
					qTipText = 'Message Read '+readDateTime;
				} else {
					var messageContainerClass = '';
					qTipText = 'Click to mark message read.';
				}				
				
				if (staffRole == 3) {
					/*
					 * This is a note from the developer
					 */
					rowClass = 'hiLite';
				} else if ((a % 2) > 0) { 
					rowClass = 'altRow'; 
				} else {	
					rowClass = '';
				}							
				
				var staffFullName = staffFName+' '+staffLName;
				var staffContactString = staffFullName+'<br />'+staffEmail+'<br />Ph: '+staffPhone;
								
				stringOut += '<div style="padding: 3px; width: 100%; cursor: pointer;" class="'+rowClass+'">';
				stringOut += '<div ext:qtip="'+qTipText+'" class="'+messageContainerClass+'" id="messageContainerDiv'+noteID+'">';
				stringOut += '<div style="float: left;"><b>'+noteDateEntry+'</b></div>';
				stringOut += '<div style="float: right; margin-right: 10px;"><a href="mailto:'+staffEmail+'" ext:qtip="'+staffContactString+'">'+staffFullName+'</a></div>';
				stringOut += '<div style="clear: both;"></div>';
				if (staffRole == 3) {
					stringOut += '<div class="iconNotes"><b>Developer Notes</b></div>';
				} else if (followUpID>0) {
					stringOut += '<div class="iconFollowUp"><b><a href="contactForm.php?contactID='+contactID+'" ext:qtip="View contact information">Follow Up Notes</a></b></div>';
				}
				stringOut += '<div style="width: 100%; cursor: pointer;" onClick="messageSupportObj.markMessageRead('+noteID+'); return false;">';
				stringOut += '<p>'+noteText+'</p>';
				stringOut += '</div>';
				stringOut += '</div></div>';
			}		
		} else {
			stringOut = '<div style="padding: 3px;">You have no messages right now.</div>';
		}
		$('windowMessagesContent').innerHTML = stringOut;
	}
}